﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SmartMD.DataModel.Entity;
using SmartMD.DataAccess.Service;
using SmartMD.Manager.Common;
using Techstar.Dialog;
using SmartMD.DataModel;

namespace SmartMD.Manager.Container
{
    /// <summary>
    /// Interaction logic for NewContainerPanel.xaml
    /// </summary>
    public partial class NewContainerPanel : UserControl
    {
        private ObservableCollection<ContainerEntity> _containerEntitys;

        public ObservableCollection<ContainerEntity> ContainerEntitys
        {
            get
            {
                if (_containerEntitys == null)
                {
                    if (SingletonManager.Online)
                    {
                        _containerEntitys = DataServiceManager.ContainerService.GetAllContainers();
                        for (int i = 0; i < _containerEntitys.Count; i++)
                        {
                            _containerEntitys[i].State = RecordState.Normal;
                        }
                        //SingletonManager.Cache.SaveEntity(SingletonManager.TemplatePath, "TemplateType", _templateTypes);
                    }
                    else if (SingletonManager.Cache.ExistsEntity(SingletonManager.TemplatePath, "TemplateType"))
                    {
                        //_containerEntitys = SingletonManager.Cache.LoadEntity<ObservableCollection<ContainerEntity>>(SingletonManager.ContainerPath, "ContainerEntity");
                    }
                    if (_containerEntitys == null)
                        _containerEntitys = new ObservableCollection<ContainerEntity>();
                }
                return _containerEntitys;
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.lbContainer.ItemsSource = ContainerEntitys;

        }

        public NewContainerPanel()
        {
            InitializeComponent();
            this.ucToolbar.newHandler = New;
           // this.ucToolbar.saveHandler = Save;
            this.ucToolbar.deleteHandler = Delete;
        }

        public static string ModuleName
        {
            get { return "新建容器"; }
        }

        private void New()
        {
            NewContainerDialog tmpDlg = new NewContainerDialog();
            tmpDlg.Owner = this;
            tmpDlg.ShowDialog();
        }

        private void Save()
        {
            if (ContainerEntitys == null)
                return;
            var items = new ObservableCollection<ContainerEntity>();
            foreach (var ctnEntity in ContainerEntitys)
            {
                if (ctnEntity.State != RecordState.Normal)
                    items.Add(ctnEntity);
            }
            if (items.Count == 0)
            {
                TaskDialogEx.Warning("没有可以提交到数据服务器的内容!");
                return;
            }

            bool isSaved = DataServiceManager.ContainerService.SaveContainers(items);
            if (isSaved)
            {
                TaskDialogEx.Information("提交", "内容提交成功!");
                for (int j = 0; j < items.Count; j++)
                {
                    items[j].State = RecordState.Normal;
                }
            }
            else
            {
                TaskDialogEx.Information("提交", "内容提交失败!");
            }
        }

        private void Delete()
        {
            ContainerEntity entity = (ContainerEntity)lbContainer.SelectedItem;
            if (entity == null)
                return;
            bool success = DataServiceManager.ContainerService.DeleteContainerById(entity.Id);
            if (success)
            {
                TaskDialogEx.Information("提交", "删除成功！");
                for (int i = 0; i < ContainerEntitys.Count; i++)
                {
                    if (entity.Guid == ContainerEntitys[i].Guid)
                        ContainerEntitys.Remove(entity);
                }
            }
        }

        private void lbContainer_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }
    }
}
